home *** CD-ROM | disk | FTP | other *** search
- // the implementation of class CKBANDoc
- // Copyright (C) 1997 Kazutaka Hirata <khirata@jove.acs.unt.edu>
-
- #include "stdafx.h"
-
- #include "bmpout.h"
- #include "dlgbmp.h"
- #include "draw.h"
- #include "drawbmp.h"
- #include "memdc.h"
- #include "netlist/netlist.h"
- #include "resource.h"
-
- #include "kbandoc.h"
-
- // member variable(s)
- IMPLEMENT_DYNCREATE(CKBANDoc, CDocument)
-
- BEGIN_MESSAGE_MAP(CKBANDoc, CDocument)
- ON_COMMAND(ID_FILE_SAVE_BMP, OnSaveAsBitmap)
- ON_COMMAND(ID_FILE_SAVE_CMP, OnSaveAsComponent)
- ON_COMMAND(ID_FILE_SEND_MAIL, OnFileSendMail)
- ON_UPDATE_COMMAND_UI(ID_FILE_SEND_MAIL, OnUpdateFileSendMail)
- ON_UPDATE_COMMAND_UI(ID_FILE_SAVE_BMP, OnUpdateFileSaveBmp)
- ON_COMMAND(ID_FILE_SAVE_GBR, OnSaveAsGerber)
- ON_COMMAND(ID_FILE_OUTPUT_NETLIST, OnOutputNetlist)
- END_MESSAGE_MAP()
-
- // constructor(s)
- CKBANDoc::CKBANDoc()
- : m_undo_history(m_info)
- {
- m_info.AssociateDocument(*this);
- }
-
- // destructor
- CKBANDoc::~CKBANDoc()
- {
- }
-
- // member functions
- bool CKBANDoc::Undo()
- {
- bool result;
- if(result = m_undo_history.Undo()) {
- m_info = m_undo_history.GetCurrent();
- }
- return result;
- }
-
- bool CKBANDoc::Redo()
- {
- bool result;
- if(result = m_undo_history.Redo()) {
- m_info = m_undo_history.GetCurrent();
- }
- return result;
- }
-
- void CKBANDoc::ClearHistory()
- {
- m_undo_history.ClearHistory();
- }
-
- void CKBANDoc::SetNewState(const char* name)
- {
- m_undo_history.SetNewState(m_info, name);
- }
-
- const const char* CKBANDoc::GetUndoName() const
- {
- return m_undo_history.GetUndoName().c_str();
- }
-
- const const char* CKBANDoc::GetRedoName() const
- {
- return m_undo_history.GetRedoName().c_str();
- }
-
- BOOL CKBANDoc::Load(LPCTSTR lpszPathName)
- {
- FILE_NEW fp(lpszPathName, "r");
- kban_info().kban_data().load(fp);
- kban_info().kban_data().collect_aperture(
- kban_info().apt_pin_table(),
- kban_info().apt_line_table()
- );
- return TRUE;
- }
-
- BOOL CKBANDoc::Save(LPCTSTR lpszPathName)
- {
- FILE_NEW fp(lpszPathName, "w");
- kban_info().kban_data().save(fp);
- return TRUE;
- }
-
- BOOL CKBANDoc::OnNewDocument()
- {
- if(!CDocument::OnNewDocument()) {
- return FALSE;
- }
- return TRUE;
- }
-
- BOOL CKBANDoc::OnOpenDocument(LPCTSTR lpszPathName)
- {
- if(!CDocument::OnOpenDocument(lpszPathName)) {
- return FALSE;
- }
- Load(lpszPathName);
- return TRUE;
- }
-
- BOOL CKBANDoc::OnSaveDocument(LPCTSTR lpszPathName)
- {
- if(!CDocument::OnSaveDocument(lpszPathName)) {
- return FALSE;
- }
- Save(lpszPathName);
- return TRUE;
- }
-
- void CKBANDoc::DeleteContents()
- {
- kban_info().kban_data().clear();
- }
-
- void CKBANDoc::Serialize(CArchive& ar)
- {
- }
-
- void CKBANDoc::SaveBitmap(KBAN_INFO& info, const SAVEASBMP_INFO& sbinfo)
- {
- FILE_NEW fp(sbinfo.m_fname, "wb");
- if(!fp.is_ok()) {
- AfxGetMainWnd()->MessageBox("Failed to open the specified file", "Save As Bitmap");
- } else {
- KBAN_DATA kban_data = info.kban_data();
- if(sbinfo.m_limit_drill.get()) {
- kban_data.limit_drill_size(sbinfo.m_limit_drill_size);
- }
- XY ac_min = kban_data.get_min();
- XY ac_max = kban_data.get_max();
-
- // adds margin
- ac_min -= DIS_DINCH;
- ac_max += DIS_DINCH;
-
- DRAW_BMP_INFO draw_info(ac_min, ac_max, DPI_DESIGN, sbinfo.m_dpi);
- XY pc_win_size = draw_info.pc_win_size();
-
- // sets up a memory DC for a bitmap
- CFrameWnd* pFrameWnd = (CFrameWnd*)AfxGetMainWnd();
- CClientDC clientDC(pFrameWnd->GetActiveView());
- CMemoryDC memDC(&clientDC, pc_win_size.x(), pc_win_size.y(), 1);
- memDC.FillSolidRect(0, 0, pc_win_size.x(), pc_win_size.y(), RGB(255, 255, 255));
-
- // draws the data
- KBAN_DRAW draw(&memDC, draw_info, sbinfo.m_fill, sbinfo.m_hole, sbinfo.m_layer);
- draw.draw_kban_data(kban_data, info.active_layer().get());
-
- OutputBMPFile(fp, memDC.GetBitmap(), clientDC);
- }
- }
-
- void CKBANDoc::OnSaveAsBitmap()
- {
- CSaveAsBitmapDialog dlg(m_sbinfo, AfxGetMainWnd());
- if(dlg.DoModal() == IDOK) {
- m_sbinfo = dlg.GetResult();
- rec << "fname = " << m_sbinfo.m_fname << "\n";
- SaveBitmap(kban_info(), m_sbinfo);
- }
- }
-
- void CKBANDoc::OnSaveAsComponent()
- {
- CString fname;
- CFileDialog dlg(FALSE, "cmp", fname,
- OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY,
- "KBAN Component Files (*.cmp)|*.cmp|All Files (*.*)|*.*||",
- AfxGetMainWnd()
- );
- if(dlg.DoModal() == IDOK) {
- fname = dlg.GetPathName();
- FILE_NEW fp(fname, "w");
- kban_info().kban_data().save(fp);
- }
- }
-
- class WIN_NETLIST_ERROR : public NETLIST_ERROR {
- public:
- WIN_NETLIST_ERROR() {};
- virtual void not_distinct(const char* comp_name);
- };
-
- void WIN_NETLIST_ERROR::not_distinct(const char* comp_name)
- {
- char mes[300];
- sprintf(mes, "Warning: The designator \"%s\" is used more than once.", comp_name);
- AfxGetMainWnd()->MessageBox(mes, "Output Netlist", MB_ICONWARNING);
- }
-
- void CKBANDoc::OnOutputNetlist()
- {
- CString fname;
- CFileDialog dlg(FALSE, "net", fname,
- OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY,
- "KBAN Netlist Files (*.net)|*.net|All Files (*.*)|*.*||",
- AfxGetMainWnd()
- );
- if(dlg.DoModal() == IDOK) {
- WIN_NETLIST_ERROR ne;
- fname = dlg.GetPathName();
- output_netlist(ne, fname, kban_info().kban_data());
- }
- }
-
- void CKBANDoc::OnUpdateFileSaveBmp(CCmdUI* pCmdUI)
- {
- pCmdUI->Enable(!kban_info().kban_data().empty());
- }
-
- void CKBANDoc::OnSaveAsGerber()
- {
- AfxGetMainWnd()->MessageBox("Hello", "OnSaveGerber");
- KBAN_INFO& info = kban_info();
- APT_TABLE apt_regular_table;
- APT_TABLE apt_drill_table;
- APT_TABLE apt_pin_table;
- APT_TABLE apt_line_table;
- info.kban_data().collect_aperture(apt_pin_table, apt_line_table);
- uint i;
- REC << "apt_pin_table\n";
- FILE_NEW fp("gerber.txt", "w");
- for(i = 0; i < apt_pin_table.size(); i++) {
- const APERTURE& current = apt_pin_table[i];
- fp.printf("type = %d, width = %4d, height = %4d, drill = %4d\n",
- current.type(),
- current.width(),
- current.height(),
- current.drill()
- );
- if(!apt_regular_table.is_included(current)) {
- apt_regular_table.push_back(current);
- }
- APERTURE apt(APERTURE::APT_ROUND, 0, 0, current.drill());
- if(!apt_drill_table.is_included(apt)) {
- apt_drill_table.push_back(apt);
- }
- }
- REC << "apt_line_table\n";
- for(i = 0; i < apt_line_table.size(); i++) {
- const APERTURE& current = apt_line_table[i];
- fp.printf("type = %d, width = %4d, height = %4d, drill = %4d\n",
- current.type(),
- current.width(),
- current.height(),
- current.drill()
- );
- if(!apt_regular_table.is_included(current)) {
- apt_regular_table.push_back(current);
- }
- }
- fp.printf("---\n");
- apt_regular_table.sort();
- for(i = 0; i < apt_regular_table.size(); i++) {
- const APERTURE& current = apt_regular_table[i];
- fp.printf("type = %d, width = %4d, height = %4d, drill = %4d\n",
- current.type(),
- current.width(),
- current.height(),
- current.drill()
- );
- }
- fp.printf("---\n");
- apt_drill_table.sort();
- for(i = 0; i < apt_drill_table.size(); i++) {
- const APERTURE& current = apt_drill_table[i];
- fp.printf("type = %d, width = %4d, height = %4d, drill = %4d\n",
- current.type(),
- current.width(),
- current.height(),
- current.drill()
- );
- }
- }
-